package graphics;

import java.awt.Point;

/**
 * A RectBoundedShape represents a shape which can be bounded in a rectangle
 * 
 * @author Jie Zhao(288654)
 * 
 */
public abstract class RectBoundedShape extends Shape {
	protected int x = 0;
	protected int y = 0;
	protected int width = 0;
	protected int height = 0;

	protected Point startPoint = new Point();
	protected Point endPoint = new Point();
	/**
	 * 
	 * @return the start drawing point of this shape
	 */
	public Point getStartPoint() {
		return startPoint;
	}

	/**
	 * set the start point of this shape
	 * 
	 * @param startPoint
	 */
	public void setStartPoint(Point startPoint) {
		if (startPoint == null)
			return;
		this.startPoint = startPoint;
		if(endPoint.x == 0 && endPoint.y == 0)
		{
			endPoint = new Point(startPoint);
		}
		calculateBoundedRect();
	}

	/**
	 * 
	 * @return end drawing point of the shape
	 */
	public Point getEndPoint() {
		return endPoint;
	}
	/**
	 * Set the end drawing point of the shape
	 * 
	 * @param endPoint
	 */
	public void setEndPoint(Point endPoint) {
		if (endPoint == null)
			return;
		this.endPoint = endPoint;
		calculateBoundedRect();
	}
	/**
	 * calculate bound rectangle using the start and end drawing point
	 */
	protected void calculateBoundedRect() {
		if (startPoint != null) {
			this.x = startPoint.x;
			this.y = startPoint.y;
		}
		if (startPoint != null && endPoint != null &&(endPoint.x !=0 || endPoint.y != 0)) {
			width = Math.abs(startPoint.x - endPoint.x); 
			height = Math.abs(startPoint.y - endPoint.y);
			x = startPoint.x < endPoint.x ? startPoint.x : endPoint.x;
			y = startPoint.y < endPoint.y ? startPoint.y : endPoint.y;
		}
	}

	/**
	 * regulate the bound rectangle
	 */
	protected void regulate() {
		width = width > height ? width : height;
		height = height > width ? height : width;
	}
}
